/**
 *    Copyright 2006-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins.custome;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Type ListJsonObject
 * @Desc 操作的json传输对象
 * @author shuai.zuo
 * @param <T>
 * @date 2012-3-23
 * @Version V1.0
 */
public class BaseJsonResponse<T> extends BaseJson {

    public BaseJsonResponse() {

    }
    
    public BaseJsonResponse(List<T> rows){
    	this.setRows(rows);
    }

    public BaseJsonResponse(PageQuery<T> query) {
        setQuery(query);
    }

    public void setQuery(PageQuery<T> query) {
        this.setPage(query.getPageIndex());
        this.setPageSize(query.getPageSize());
        this.setTotal(query.getTotalRecord());
        this.setRows(query.getDataList());
        this.setTotalPage(query.getTotalPage());
        this.setStartPos(query.getStartPos());
        this.setEndPos(query.getEndPos());
    }

    public  <T> void   handlePageResult(Result<PageQuery<T>> pageQueryResult, BaseJsonResponse<T> baseJsonResponse) {
        if (pageQueryResult.isSuccess()) {
            baseJsonResponse.setQuery(pageQueryResult.getData());
            baseJsonResponse.setCode(RestResponseCode._200);
        } else {
            baseJsonResponse.setErrorMessage(pageQueryResult.getMsg());
            baseJsonResponse.setHasError(true);
            baseJsonResponse.setCode(RestResponseCode._500);
        }
    }

    /**
     * 对象
     */
    private List<T> rows;
    /**
     * 总记录数
     */
    private int total;
    /**
     * 总页数
     */
    protected int totalPage;
    /**
     * 一页大小
     */
    protected int pageSize;
    /**
     * 当前页数，从 0开始，0代表第一页
     */
    protected int page;
    protected int startPos;
    protected int endPos;

    private final Map<String, Object> paramsMap = new HashMap<String, Object>();

    /**
     * @return the rows
     */
    public List<T> getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getStartPos() {
        return startPos;
    }

    public void setStartPos(int startPos) {
        this.startPos = startPos;
    }

    public int getEndPos() {
        return endPos;
    }

    public void setEndPos(int endPos) {
        this.endPos = endPos;
    }

    public Map<String, Object> getParamsMap() {
        return paramsMap;
    }

}
